const {getMyInfo, volunteerModify} = require('../../services/myInfoReq');
const {areaList} = require('../../utils/pablicInfo');
const moment = require('../../utils/moment');
const {getItem} = require('../../utils/storge');


Page({
    data: {
        myInfoObj: {},
        EditorNow: false,
        areaList: areaList,
        EditorNowAddress: false,
        EditorNowAge: false,
        EditorNowSex: false,
        currentDate: new Date().getTime(),
        minDate: new Date(1920, 1, 1).getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
    },

    implementModifyMyInfo(
        target,
        value,
    ) {
        let myInfoObj = this.data.myInfoObj;
        myInfoObj[target] = value;
        this.setData({
            myInfoObj,
        });
    },
    trigger(
        target,
    ) {
        let triggerObj = this.data[target];
        triggerObj = !triggerObj;
        this.setData({
            [target]: triggerObj,
        });
    },
    onLoad: function (options) {
        wx.showLoading({
            title: '加载中',
            mask: true,
        });

        const volunteerInfo = getItem();
        getMyInfo(volunteerInfo.volunteerAccount, res => {
            this.setData({
                myInfoObj: res.data.data,
            });
            wx.hideLoading();
        });
    },

    handleEditorInfo() {
        return this.trigger('EditorNow');
    },
    onCancelSelectAddress() {
        return this.trigger('EditorNowAddress');
    },

    onConfirmSelectAge(e) {
        const {detail: age} = e;
        this.implementModifyMyInfo('volunteerAge', moment(age).format('YYYY-MM-DD'));
        return this.trigger('EditorNowAge');
    },
    onConfirmSelectSex(e) {
        const {name: sex} = e.detail;
        this.implementModifyMyInfo('volunteerSex', sex);
        return this.trigger('EditorNowSex');
    },
    onCloseAge() {
        return this.trigger('EditorNowAge');
    },
    onClose() {
        return this.trigger('EditorNowSex');
    },

    onConfirmSelectAddress(e) {
        const {values: data} = e.detail;
        const address = `${data[0].name}/${data[1].name}/${data[2].name}`;
        const townCode = `${data[0].name}${data[1].name}${data[2].name}`;
        this.implementModifyMyInfo('volunteerAddress', address);
        this.implementModifyMyInfo('townCode', townCode);
        return this.trigger('EditorNowAddress');

    },
    handleSelectAdress() {
        let EditorNow = this.data.EditorNow;
        if (!EditorNow) return;
        return this.trigger('EditorNowAddress');
    },
    handleSelectAge() {
        let EditorNow = this.data.EditorNow;
        if (!EditorNow) return;
        return this.trigger('EditorNowAge');
    },
    handleSelectSex() {
        let EditorNow = this.data.EditorNow;
        if (!EditorNow) return;
        return this.trigger('EditorNowSex');
    },
    handleChangeName(e) {
        const {detail: volunteerName} = e;
        return this.implementModifyMyInfo('volunteerName', volunteerName);

    },
    handleChangeOccupation(e) {
        const {detail: volunteerOccupation} = e;
        return this.implementModifyMyInfo('volunteerName', volunteerOccupation);
    },
    handleChangeSchool(e) {
        const {detail: volunteerSchool} = e;
        return this.implementModifyMyInfo('volunteerSchool', volunteerSchool);
    },
    handleChangeSpecialty(e) {
        const {detail: volunteerSpecialty} = e;
        this.implementModifyMyInfo('volunteerSpecialty', volunteerSpecialty);
    },

    handleChangeIdentify(e) {
        const {detail: volunteerIdentify} = e;
        this.implementModifyMyInfo('volunteerIdentify', volunteerIdentify);
    },
    handleChangeNo(e) {
        const {detail: volunteerNo} = e;
        this.implementModifyMyInfo('volunteerNo', volunteerNo);

    },
    handleUpdateInfo(e) {
        wx.showLoading({
            title: '正在提交信息',
            mask: true,
        });
        let params = this.data.myInfoObj;
        volunteerModify(params, res => {
            wx.showToast({
                title: '修改成功',
                type: 'success',
                mask: true,
            });
            this.trigger('EditorNow');
        });
    },
});
